import store from '@/store/'
import * as types from '@/store/mutation-types'
export default (msg, url) => {
  if (!msg) {
    console.error('接口返回数据结构无法解析', msg)
    return false
  }
  switch (parseInt(msg.returnCode)) {
    case 10000: // 默认正确的code
    // 以下为特殊处理,需要放到控制层处理的错误code,必须注释使用页面和错误行为
    case 31100:
    case 31102:
    // case 10500: //文章解析异常(一个输入框填写一个具有正确域名地址却不是正确的文章地址问题)
    case 31106: //同个人不可解析同一篇文章
    case 21105: // 注册
    // case 21103: // 验证码错误
    case 21801: // 开通楼盘vip到期
    case 10028: // 文章被下架或被删除
    case 44006: // 您暂无权限参与该活动
    case 44007: // 该活动已结束，谢谢参与
    case 44009: // 该活动未开始，敬请关注
    case 44011: // 手机号码已领取奖励，请勿重复参加
      return true
    case 10039: //用户被删除
      var userStatus_10039 = sessionStorage.getItem("userStatus_10039");
      if (userStatus_10039) {
        return false;
      }
      sessionStorage.setItem("userStatus_10039", 'userStatus_10039');
      vue.$router.replace({ path: "/", query: { userStatus: 10039 } })
      return false;
    case 10040: //用户被禁用
      var userStatus_10040 = sessionStorage.getItem("userStatus_10040");
      if (userStatus_10040) {
        return false;
      }
      sessionStorage.setItem("userStatus_10040", 'userStatus_10040');
      vue.$router.replace({ path: "/", query: { userStatus: 10040 } })
    case 21600: // 无分享记录的时候调的接口错误，不提示
      return false
    case 40029: //通过code获取用户信息失败
      window.vue.$toast(msg.msg)
      return false
    case 101: //token失败
    case 102:
    case 103:
    case 104:
    case 105:
    case 106:
      //防止接口循环调用
      var isTokenExpired = sessionStorage.getItem("isTokenExpired");
      if (isTokenExpired) {
        return false;
      }
      if (!(process.env.VUE_APP_AW_ENV == 'uat' || process.env.VUE_APP_AW_ENV == 'prod' || location.href.indexOf('sit') > -1)) {
        return true;
      }

      sessionStorage.setItem("isTokenExpired", 'isTokenExpired');
      let wxredirecturl = window.location.href.split('#')[0].split('?')[0]
      wxredirecturl = wxredirecturl.substr(0, wxredirecturl.length - 1)
      store.dispatch('getUserInfo', null)
      store.commit(types.WX_JSSDK, null)

      let cropId = localStorage.getItem('cropId');
      localStorage.removeItem("awMasterUserInfo_sass_" + cropId);

      // let wxurl =
      //   'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
      //   cropId +
      //   '&redirect_uri=' +
      //   encodeURIComponent(wxredirecturl).toLowerCase() +
      //   '&response_type=code&scope=snsapi_userinfo&state=062882&component_appid=' + component_appid + '#wechat_redirect'
      // console.log(wxredirecturl)

      // 授权地址
      let wxurl = ''
      if (sessionStorage.getItem('userLoginType') == '1') {
        wxurl =
          'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
          cropId +
          '&redirect_uri=' +
          encodeURIComponent(wxredirecturl).toLowerCase() +
          '&response_type=code&scope=snsapi_userinfo&state=062882#wechat_redirect';
      } else {
        let component_appid = process.env.VUE_APP_Component_Appid
        wxurl =
          'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
          cropId +
          '&redirect_uri=' +
          encodeURIComponent(wxredirecturl).toLowerCase() +
          '&response_type=code&scope=snsapi_userinfo&state=062882&component_appid=' + component_appid + '#wechat_redirect';
      }
      window.location.href = wxurl
      return false
    case 107:
      // 公众号被禁用
      vue.$router.replace('/wx/error')
    default:
      console.error(msg)
      window.vue.$toast(msg.msg)
      return false
  }
}
